package cn.yuan.xiaoyu.wangmodule.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import butterknife.Bind;
import cn.yuan.xiaoyu.R;
import cn.yuan.xiaoyu.commonmodule.fragment.BaseFragment;
import cn.yuan.xiaoyu.commonmodule.view.FastIndexBar;
import cn.yuan.xiaoyu.wangmodule.adapter.WangWangContactsAdapter;
import cn.yuan.xiaoyu.wangmodule.bean.ContactsBean;
import cn.yuan.xiaoyu.wangmodule.utils.DataUtil;

/**
 * Created by yukuoyuan on 2017/3/2.
 * 这是一个旺旺的通讯录的界面
 */

public class WangWangContactFragment extends BaseFragment {
    @Bind(R.id.rcv_wangwang_contacts)
    ListView rcvWangwangContacts;
    private View headView;
    @Bind(R.id.fib_ww_main_contacts)
    FastIndexBar fib_ww_main_contacts;
    @Bind(R.id.tv_ww_main_contacts_tip)
    TextView tv_ww_main_contacts_tip;

    @Override
    public void initdata(Bundle arguments) {
        rcvWangwangContacts.addHeaderView(getHeadView());
        final List<ContactsBean> contactsBeanList = DataUtil.getInstance().getData();
        //排序
        Collections.sort(contactsBeanList, new Comparator<ContactsBean>() {
            @Override
            public int compare(ContactsBean lhs, ContactsBean rhs) {
                return lhs.getPinyin().compareToIgnoreCase(rhs.getPinyin());
            }
        });
        WangWangContactsAdapter wangWangContactsAdapter = new WangWangContactsAdapter(contactsBeanList, getActivity());
        rcvWangwangContacts.setAdapter(wangWangContactsAdapter);
        fib_ww_main_contacts.setOnLetterChangeListener(new FastIndexBar.OnLetterChangeListener() {
            @Override
            public void onLetterChange(String letter) {
                if (contactsBeanList != null) {
                    tv_ww_main_contacts_tip.setVisibility(View.VISIBLE);
                    tv_ww_main_contacts_tip.setText(letter);
                    for (int i = 0; i < contactsBeanList.size(); i++) {
                        if (letter.equals(contactsBeanList.get(i).getPinyin())) {
                            rcvWangwangContacts.setSelection(i);
                            break;
                        }
                    }
                }
            }

            @Override
            public void onUp() {
                tv_ww_main_contacts_tip.setVisibility(View.GONE);
            }
        });
    }

    @Override
    public int getContentLayout() {
        return R.layout.fragment_wangwang_contact;
    }

    @Override
    protected void bindPresenter() {

    }

    @Override
    protected void initToolbar() {

    }

    /**
     * 这是一个初始化headView的方法
     *
     * @return
     */
    public View getHeadView() {
        View headView = View.inflate(getActivity(), R.layout.headview_wangwang_contact, null);
        return headView;
    }
}
